package com.chen.springboot.controller;

import com.chen.springboot.common.Result;
import com.chen.springboot.entity.RefundOrder;
import com.chen.springboot.mapper.ItemMapper;
import com.chen.springboot.mapper.OrderMapper;
import com.chen.springboot.mapper.RefundOrderMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private RefundOrderMapper refundOrderMapper;

    @Resource
    private ItemMapper itemMapper;

    /**
     * 订单退款接口
     */
    @PostMapping("/refunding")
    public Result<?> refunding(@RequestBody RefundOrder refundOrder) {
        // 创建退款响应
        // 首先将这个订单的状态改为已退款，不能单纯通过一个订单ID来判断，因为可能是多个商品
        // 直接使用订单ID来更改就行
        if (orderMapper.updateRefundState(refundOrder.getId()) >= 0) {
            System.out.println("更新退款订单状态成功");
        } else {
            System.out.println("更新退款订单状态失败");
            return Result.error("500", "请与管理员联系");
        }
        // 然后更新退款时间
        refundOrder.setRefundDate(new Date());
        // 然后在退款订单表里面保存退款订单
        refundOrderMapper.insert(refundOrder);
        return Result.success();
    }
}
